package com.ficus.road.maintain.strategy.allfactory;

import com.ficus.road.maintain.core.model.*;
import com.ficus.road.maintain.core.model.zh.IllegalDiggingZH;
import com.ficus.road.maintain.dao.HitAllFactoryDao;
import com.ficus.road.maintain.util.MyDateUtil;
import org.apache.commons.lang3.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.ficus.road.maintain.core.model.HitStatusEnum.NEW;
import static com.ficus.road.maintain.core.model.HitStatusEnum.NOTED;

/**
 * @author xiawen yang
 * @date 2022/4/7 下午1:39
 */
public class IllegalDiggingStrategy implements AllFactoryStrategy<IllegalDiggingZH> {

    private static final String type = AllFactoryTypeEnum.ILLEGAL_DIGGING.getType();

    private HitAllFactoryDao hitAllFactoryDao;

    public IllegalDiggingStrategy(HitAllFactoryDao hitAllFactoryDao) {
        this.hitAllFactoryDao = hitAllFactoryDao;
    }

    @Override
    public Hit convert2AllFactoryHit(IllegalDiggingZH zh) {
        String findTimeStr = zh.getFindtime();
        Date findTime = MyDateUtil.strParseDate(findTimeStr, "yyyy-MM-dd HH:mm:ss");
        GpsLocation gpsLocation = GpsLocation.builder().latitude(31.235870d).longitude(121.621148d).build();
        String source = "MOCK";
        if (ObjectUtils.isNotEmpty(zh.getLat()) && ObjectUtils.isNotEmpty(zh.getLgt())) {
            gpsLocation = GpsLocation.builder().latitude(Double.parseDouble(zh.getLat())).longitude(Double.parseDouble(zh.getLgt())).build();
            source = "NORMAL";
        }

        Hit hit = new Hit();
        HitAttrs hitAttrs = new HitAttrs();
        hitAttrs.setConfirmation(Confirmation.builder().build());
        hitAttrs.setSource(source);
        hit.setScene("CITY");
        hit.setAttrs(hitAttrs);
        hit.setCreationTime(new Date().getTime() / 1000);
        hit.setFirstHitTime(findTime.getTime() / 1000);
        hit.setLastModificationTime(new Date().getTime() / 1000);
        hitAttrs.setRoadName(zh.getPosition());
        hitAttrs.getConfirmation().setStatus(Confirmation.ConfirmationStatus.CORRECT);
        hitAttrs.setFrameImageUrl(zh.getPatrolPicture());
        hitAttrs.setSceneImage(zh.getPatrolPicture());
        hitAttrs.setHandleCompany(zh.getMaintaincompany());
        hitAttrs.setSection(zh.getMaintainsection());
        hitAttrs.setDescription(zh.getRemark());
//            hitAttrs.setLimitHour(null);
        hitAttrs.setGpsLocation(gpsLocation);
        hit.setId(zh.getEventsid());
        hit.setType(type);
        hit.setNeedPushOut(false);
        List<HistoryHit> historyHits = new ArrayList<>();
        HistoryHit historyHit = new HistoryHit();
        historyHits.add(historyHit);
        HistoryHitAttrs historyHitAttrs = new HistoryHitAttrs();
        historyHit.setAttrs(historyHitAttrs);
        historyHit.setHitTime(hit.getFirstHitTime());
        historyHitAttrs.setStatus(NEW);
        historyHitAttrs.setSceneImage(hit.getAttrs().getSceneImage());

        //提醒件增加提醒状态
        HistoryHit notedHistoryHit = new HistoryHit();
        historyHits.add(0,notedHistoryHit);
        HistoryHitAttrs notedHistoryHitAttrs = new HistoryHitAttrs();
        notedHistoryHit.setAttrs(notedHistoryHitAttrs);
        notedHistoryHit.setHitTime(hit.getFirstHitTime());
        notedHistoryHitAttrs.setStatus(NOTED);
        notedHistoryHitAttrs.setSceneImage(hit.getAttrs().getSceneImage());

        hit.getAttrs().setStatus(NOTED);

        hit.setList(historyHits);
        return hit;
    }
}
